package pers.csw.lookBook.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import pers.csw.lookBook.common.Res;
import pers.csw.lookBook.entity.User;
import pers.csw.lookBook.service.UserService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author csw
 */
@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController {
    final UserService userService;
    @Autowired
    StringRedisTemplate redisTemplate;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 注册
     * @param user 用户对象
     * @return 返回是否成功
     */
    @PostMapping("/register/{uuid}/{userCode}")
    public Res register(@RequestBody User user,@PathVariable String uuid,@PathVariable String userCode){
        String key = "userUUID"+uuid;
        String s = redisTemplate.opsForValue().get(key);
        if(userCode.equals(s)){
            redisTemplate.delete(key);
            return userService.register(user);
        }else{
            return new Res("500","验证码有误");
        }

    }

    /**
     * 登录
     * @param user 用户对象
     * @return 返回用户信息
     */
    @PostMapping("/login/{uuid}/{userCode}")
    public Res login(@RequestBody User user,@PathVariable String uuid,@PathVariable String userCode){
        String key = "userUUID"+uuid;
        String s = redisTemplate.opsForValue().get(key);
        if(userCode.equals(s)){
            redisTemplate.delete(key);
            return userService.login(user.getUsername(), user.getPassword());
        }else{
            return new Res("500","验证码有误");
        }
    }

    /**
     * 通过用户id返回该用户的信息
     * @param id 用户id
     * @return 返回用户信息
     */
    @SaCheckLogin
    @GetMapping("/getUserInfo/{id}")
    public Res getUserInfoById(@PathVariable("id") Integer id){
        return userService.getUserInfo(id);
    }

    /**
     * 修改用户信息
     * @param user 用户对象
     * @return 返回是否成功
     */
    @SaCheckLogin
    @PutMapping("/updateUserInfo")
    public Res updateUserInfo(@RequestBody User user){
        return userService.updateUser(user);
    }

    /**
     * 通过用户id获取书架信息
     * @param userId 用户id
     * @return 书架信息
     */
    @SaCheckLogin
    @GetMapping("/getBookRack/{userId}")
    public Res getBookRackByUserId(@PathVariable("userId") Integer userId){
       return userService.getBookRackByUserId(userId);
    }


    @PutMapping("/updateImg")
    public Res updateImg(@RequestBody User user){
        System.out.println(user);
        String imgUrl = user.getImgUrl();
        boolean update = userService.updateById(user);
        return Res.success(update);
    }

    @RequestMapping("/getCode")
    public Res getCode(HttpSession session) {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
        String s = IdUtil.simpleUUID();;
        HashMap code = new HashMap();
        code.put("img",captcha.getImageBase64());
        code.put("uuid", s);
        redisTemplate.opsForValue().set("userUUID"+s,captcha.getCode(),60, TimeUnit.SECONDS);
        return Res.success(code);
    }



    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
